home *** CD-ROM | disk | FTP | other *** search
- Path: newspost1.alt.net!usenet
- From: walth@netcom.com (Walt Howard)
- Newsgroups: comp.lang.c++
- Subject: Re: Copy constructing an already default constructed object
- Date: Wed, 31 Jan 1996 08:18:35 GMT
- Organization: AltNet - Affordable Usenet Access - http://www.alt.net
- Message-ID: <4en8l1$35e@tofu.alt.net>
- References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com> <3108ef14.340699@nntp> <4ebehj$5i7@news.bridge.net> <4edr59$87h@elaine11.Stanford.EDU>
- Reply-To: walth@netcom.com
- X-Newsreader: Forte Agent .99c/32.126
-
- On 27 Jan 1996 10:33:13 -0800, brien@leland.Stanford.EDU (brien
- oberstein) wrote:
-
- >David Byrden <100101.2547@compuserve.com> writes:
- >
- >
- >>>> A::operator =(const A& other)
- >>>> {
- >>>> b0 = other.b0 ;
- >>>> b1 = other.b1;
- >>>> }
- >
- >>>> To me this seems like a major pain in the butt.
- >
- >>That's precisely why the compiler will automatically generate a public
- >>assignment operator with this behaviour, if you don't bother to write
- >>one.
- >
- >Bzz. Try again king shit. The compiler generates memberwise assignment.
- >
- >
-
- Huh? That's what he is saying. b0 and b1 are MEMBERS of A and
- the compiler will automatically generate:
-
- b0 = other.b0 ;
- b1 = other.b1;
-
- i.e., memberwise assignment.
-
- >>>> Do you have to overload = for all the contained operators too?
- >
-
- No, because there is always an = operator for a class, be it compiler
- generated, explicitly written or hidden as a private member.
-
- Pointers are the buggaboo here. Usually you don't want the POINTERS
- copied. You want what they REFER TO copied, i.e., the REFERANT. In
- that case you have to explicitly code to make that happen.
-
- It's the same in C. You can't copy strings by = ing their pointers right?
- You have to use strcpy(destination, source )
-
- >>>> All I really want to do is have the copy constructor invoked on the
- >>>> piece that piece of memory. Why can't this be done?
-
- Because = an object, means = all it's components, not, = it's memory.
- In C++ an OBJECT is not necessarily just memory. I can be a "connection"
- to another machine. It can be an open file. It can be a screen
- capture. It could be a TCP/IP port. Whatever. The only thing the
- compiler can safely do is copy the components that make up an object.
- That's what the compiler generated assignment operator does.
-
- If you want to override that you can. Just copy to the "this" pointer.
-
- CLASS& operator=( CLASS &other )
- {
- memcpy( this, &other, sizeof( CLASS ) );
- }
-
- This is terrible C++ though. Just don't do it. Take my word for it.
-
-
-
-